# Maintainer: Maksim Bondarenkov <maksapple2306@gmail.com>

_realname=edit
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.0.0
pkgrel=1
pkgdesc="Terminal text editor from Microsoft (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url='https://github.com/microsoft/edit'
license=('spdx:MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-rust"
             "${MINGW_PACKAGE_PREFIX}-rust-src")
source=("${url}/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "001-dont-apply-msvc-flags.patch")
sha256sums=('6718d9a96aa9dd2117d9d4afa83b0278ef007bcd42bdb1e8c4e2ad34144246c0'
            '1a70087f6a978c53a5e57e72347b524a7813ed6a48abd852a1c77ac8621c876f')

prepare() {
  cd "${_realname}-${pkgver}"

  # don't apply msvc specific cflags so .cargo/release.toml can be used
  patch -Np1 -i ../001-dont-apply-msvc-flags.patch

  cargo fetch --locked --target "$(rustc -vV | sed -n 's/host: //p')"
  RUSTC_BOOTSTRAP=1 cargo fetch --locked --target "$(rustc -vV | sed -n 's/host: //p')" \
    --manifest-path="${MINGW_PREFIX}/lib/rustlib/src/rust/library/std/Cargo.toml"
}

build() {
  cd "${_realname}-${pkgver}"

  # FIXME: split debuginfo is not supported for mingw host anyway
  # https://github.com/rust-lang/rust/blob/60dabef95a3de3ec974dcb50926e4bfe743f078f/compiler/rustc_target/src/spec/base/windows_gnu.rs#L111
  export CARGO_PROFILE_RELEASE_DEBUG=0
  RUSTC_BOOTSTRAP=1 \
    cargo build \
    --config .cargo/release.toml \
    --release \
    --frozen
}

package() {
  cd "${_realname}-${pkgver}"

  RUSTC_BOOTSTRAP=1 \
    cargo install \
      --config .cargo/release.toml \
      --offline \
      --no-track \
      --frozen \
      --path . \
      --root "${pkgdir}${MINGW_PREFIX}"

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
